 /*
 * UMLSynch - VPSDE plugin part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.vpsde.rpc.VPSDEDiagramManager;

import java.util.Iterator;

import com.vp.plugin.ApplicationManager;
import com.vp.plugin.action.VPContext;
import com.vp.plugin.diagram.IDiagramElement;
import com.vp.plugin.model.IAttribute;
import com.vp.plugin.model.IClass;
import com.vp.plugin.model.IModelElement;
import com.vp.plugin.model.IOperation;

public class VPClassFieldVisibilityRulesManager {

	public void classApplyHerRule(VPContext context, IClass model, String[] rul) {
		if (rul.length <4)
			return;
		ApplicationManager.instance().getViewManager().showMessage("rul2 " + rul[1]);
		
		IDiagramElement elem = null; // this.getClassReference(context, rul[4]);

		if (elem == null)
			return;
		
		IModelElement reqModeElement = elem.getModelElement();
		IClass herClass;

		// Action applicable for Classes, yet
		if (reqModeElement instanceof IClass) {
			herClass = (IClass) reqModeElement ;		
		} else {
			return;
		}
			
       	boolean isVisible = (rul[1].equalsIgnoreCase("inc"));
       	//rul_her:inc/exc:meth/field/*:public/private/protected/*:ClassName/Interface
       	if (rul[2].equalsIgnoreCase("*") || rul[2].equalsIgnoreCase("meth")) {
       		ApplicationManager.instance().getViewManager().showMessage("rul3 " + rul[2]);
       		Iterator iter = model.operationIterator();
       		while (iter.hasNext())
       		{
    			IOperation oper = (IOperation)iter.next();
       			ApplicationManager.instance().getViewManager().showMessage("type modifier:" + oper.getVisibility());
    			if (oper.getVisibility().equalsIgnoreCase(rul[3]) || rul[3].equalsIgnoreCase("*")) {
    				ApplicationManager.instance().getViewManager().showMessage("rul4 " + rul[4]);
					//if (isClassOperationAvailable(herClass, oper.getName())) {
//						oper.setVisible(isVisible);
    					//ApplicationManager.instance().getViewManager().showMessage("rul5 ");
//					}
    				
					ApplicationManager.instance().getViewManager().showMessage("rul6 ");
    					
    			}    				
       		}
       	}

       	if (rul[2].equalsIgnoreCase("*") || rul[2].equalsIgnoreCase("field")) {
       		Iterator iter = model.attributeIterator();
       		while (iter.hasNext())
       		{
    			IAttribute attr = (IAttribute)iter.next();
    			if (attr.getTypeModifier().equalsIgnoreCase(rul[3]) || rul[3].equalsIgnoreCase("*")) {
				//	if (isClassOperationAvailable(herClass, attr.getName()))
					//	attr.setVisible(isVisible);
    			}    				
       		}
       	}       	
	}
	
	public void classApplyNameRule(IClass model, String[] rul) {
		if (rul.length <4)
			return;
		ApplicationManager.instance().getViewManager().showMessage("rul2 " + rul[1]);
		
       	boolean isVisible = (rul[1].equalsIgnoreCase("inc"));
       	if (rul[2].equalsIgnoreCase("*") || rul[2].equalsIgnoreCase("meth")) {
       		ApplicationManager.instance().getViewManager().showMessage("rul3 " + rul[2]);
       		Iterator iter = model.operationIterator();
       		while (iter.hasNext())
       		{
    			IOperation oper = (IOperation)iter.next();
       			ApplicationManager.instance().getViewManager().showMessage("type modifier:" + oper.getVisibility());
    			if (oper.getVisibility().equalsIgnoreCase(rul[3]) || rul[3].equalsIgnoreCase("*")) {
    				ApplicationManager.instance().getViewManager().showMessage("rul4 " + rul[4]);
    				if (oper.getName().contains(rul[4])) {
    					oper.setVisible(isVisible);
    					ApplicationManager.instance().getViewManager().showMessage("rul5 ");    					
    				}
    				if (oper.getName().indexOf(rul[4]) > 0) {
    					oper.setVisible(isVisible);
    					ApplicationManager.instance().getViewManager().showMessage("rul5 ");    					
    				}
    				ApplicationManager.instance().getViewManager().showMessage("rul6 ");
    					
    			}    				
       		}
       	}

       	if (rul[2].equalsIgnoreCase("*") || rul[2].equalsIgnoreCase("field")) {
       		Iterator iter = model.attributeIterator();
       		while (iter.hasNext())
       		{
    			IAttribute attr = (IAttribute)iter.next();
    			if (attr.getTypeModifier().equalsIgnoreCase(rul[3]) || rul[3].equalsIgnoreCase("*")) {
    				if (attr.getName().contains(rul[4]))
    					attr.setVisible(isVisible);
    			}    				
       		}
       	}       	
	}
	
	public void performApplyRules(VPContext context) {
		IDiagramElement selectedElement = context.getDiagramElement();
		IClass selectedClass;
		IModelElement selectedModeElement = selectedElement.getModelElement();
		
		// Action applicable for Classes, yet
		if (selectedModeElement instanceof IClass) {
			selectedClass = (IClass) selectedModeElement ;
		} else 
			return;
        String specList = selectedClass.getDocumentation();
        String[] specSplitList = specList.split(";");
        
        for (int i = 0; i < specSplitList.length; i++)
        {
        	String[] rul = specSplitList[i].split(":");
        	// rul_{name/heritage}:{inc/exc}:{meth/field/*}:{public/private/protected}:{$Name}
    		ApplicationManager.instance().getViewManager().showMessage("rul1:" + rul[0]);
        	if (rul[0].equalsIgnoreCase("rul_name")) {
        		classApplyNameRule(selectedClass, rul);
        	}
        	if (rul[0].equalsIgnoreCase("rul_her")) {
        		classApplyHerRule(context, selectedClass, rul);
        	}
        }

	}
}
